import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Home from '@/view/home'
import Login from '@/view/login'
import Register from '@/view/register'
import ResetPassword from '@/view/resetPwd'
import MainPage from '@/view/main'
import screening from '@/components/screening';
import result from '@/components/result';
import detail from '@/view/detail';
import main2 from '@/view/main2';
import screening2 from '@/components/screening2';
import result2 from '@/components/result2';
import detail2 from '@/view/detail2';
import login2 from '@/view/login2';

Vue.use(Router)

const router = new Router({
    routes: [
        // {
        //   component: HelloWorld,
        //   path: '/',
        //   name: 'HelloWorld'
        // },
        // {
        //   component: Home,
        //   path: '/home'
        // },
        {
            component: main2,
            path: '/',
            name: 'main2',
            redirect: '/login2',
            children: [
                {
                    path: 'login2',
                    component: login2,
                    name: 'login2'
                },
                {
                    path: 'screening2',
                    component: screening2,
                    name: 'screening2',
                    meta: {
                        requireAuth: true
                    }
                },
                {
                    path: 'result2',
                    component: result2,
                    name: 'result2',
                    meta: {
                        requireAuth: true
                    }
                }
            ]
        },
        // {
        //   component: Home,
        //   path: '/',
        //   name: 'home'
        // },
        {
            component: Login,
            path: '/login',
            name: 'login'
        },
        {
            component: Register,
            path: '/register'
        },
        {
            component: ResetPassword,
            path: '/resetPwd'
        },
        {
            component: MainPage,
            path: '/main',
            meta: {
                requireAuth: true
            },
            redirect: '/main/screening',
            children: [
                {
                    path: 'screening',
                    component: screening,
                    name: 'screening',
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: 'result',
                    component: result,
                    name: 'result',
                    meta: {
                        requireAuth: true
                    },
                }
            ]
        },
        {
            component: detail,
            path: '/detail/:id',
            name: 'detail',
            meta: {
                requireAuth: true
            }
        },
        {
            component: detail2,
            path: '/detail2/:id',
            name: 'detail2'
        }
    ]
})


router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        if (window.localStorage.getItem('screeningLogin')) {
            next();
        } else {
            next({
                path: '/login2',
                query: {redirect: to.fullPath}
            })
        }
    } else {
        next()
    }
})

export default router;
